<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
session_start();
require 'login_reg.php';

$login = new login_reg();
if (isset($_POST['LoginButton'])){
   // echo $_POST['UserNameLogin'];
   // echo $_POST['PasswordLogin'];
   
   $u = $_POST['UserNameLogin'];
   $p = $_POST['PasswordLogin'];

   if ($login->validation_login($_POST['UserNameLogin'], $_POST['PasswordLogin']) == true){
       $q = "SELECT nama_lengkap FROM user WHERE usr_email='$u'";
       $login->exec_q($q, "nama_lengkap");

       $_SESSION['Username'] = $u;
       $_SESSION['NamaLengkap'] = $login->result[0];
       $_SESSION['Password'] = $p;

       //cek dulu role dari username
       $query = "SELECT role FROM user WHERE usr_email='$u'";

       $login->exec_q($query, "role");

       if ($login->result[0] == 1){
           //role admin
           header("Location: ../admin.php");
       }
       else{
           //role user biasa
           header("Location: ../homepage.php");
           $login->setOnline($_SESSION['Username'], 1);
       }


   }
   else{
        header('Location: ../index.php?gagal=1');
   }

}

if ($_GET['logout'] == 1){
    
    //delete pesan dulu
    $del = new login_reg();
    $id = $_SESSION['gid'];

    $q_del = "DELETE FROM chat WHERE group_id='$id'";
    $q_del_g = "DELETE FROM group_members WHERE group_id='$id'";
    $q_del_gl = "DELETE FROM group_list WHERE group_id='$id'";
    $q_del_n = "DELETE FROM notification WHERE photo_id='$id'";
    $del->connect();
    mysql_query($q_del, $del->conn);
    mysql_query($q_del_g, $del->conn);
    mysql_query($q_del_gl, $del->conn);
    mysql_query($q_del_n, $del->conn);

    mysql_close();

    //delete group member

    //delete group list
    //delete notif

    session_destroy();
    header('Location: ../index.php');
    $login->setOnline($_SESSION['Username'], 0);
}

?>
